from vec import Vec2
import math
import random
import pgzrun
WIDTH = 1000
HEIGHT = 1000

PEN_COLOR = (0, 200, 0)
BACK_COLOR = (0, 0, 0)
ptList = []
length = 400
pt_mid = Vec2(WIDTH / 2, HEIGHT / 2)
pt_a = Vec2(pt_mid.x - 100, pt_mid.y - 100)
pt_b = Vec2(pt_mid.x + 100 , pt_mid.y - 100)
pt_c = Vec2(pt_mid.x + 100, pt_mid.y + 100)
pt_d = Vec2(pt_mid.x - 100,pt_mid.y + 100)

ptList.append(pt_a)
ptList.append(pt_b)
ptList.append(pt_c)
ptList.append(pt_d)

def fractal(ptList):
    i = 0
    while True:
        if i == len(ptList) - 1:
            line_fractal(ptList, i, 0)            
            break
        else:
            i = line_fractal(ptList, i, i + 1)
    
def line_fractal(ptList, a, b):
    dir = ptList[b] - ptList[a]
    plus = dir.length() / 4
    dir.normalize()
    vleftdir = Vec2(0, 0)
    pt1 = ptList[a] * 3 / 4 + ptList[b] / 4
    vleftdir.x = -dir.y
    vleftdir.y = dir.x

    vturndir = Vec2(0,0)
    pt2 = pt1 + vleftdir * plus
    vturndir.x = vleftdir.y
    vturndir.y = -vleftdir.x
    
    vrightdir = Vec2(0,0)
    pt3 = pt2 + vturndir * plus
    vrightdir.x = vturndir.y
    vrightdir.y = -vturndir.x
    # vdir.x = dir.y
    # vdir.y = -dir.x

    # vdir.x = -dir.y
    # vdir.y = dir.x
    pt4 = pt3 + vrightdir * plus
    pt5 = pt4 + vrightdir * plus
    pt6 = pt5 + vturndir * plus
    pt7 = pt6 + vleftdir * plus
    ptList.insert(a + 1, pt7)
    ptList.insert(a + 1, pt6)
    ptList.insert(a + 1, pt5)
    ptList.insert(a + 1, pt4)
    ptList.insert(a + 1, pt3)
    ptList.insert(a + 1, pt2)
    ptList.insert(a + 1, pt1)
    return  a + 8

for i in range(4):
    fractal(ptList)

def draw():
    screen.fill(BACK_COLOR)
    for i in range(len(ptList)):
        if i == len(ptList) - 1:
            screen.draw.line(ptList[i].raw_get(), ptList[0].raw_get(), PEN_COLOR)
#            print(ptList[i].raw_get())
        else:
            screen.draw.line(ptList[i].raw_get(), ptList[i + 1].raw_get(), PEN_COLOR)    
#            print(ptList[i].raw_get())
pgzrun.go()